#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
 #include <QQmlEngine>
#include <QtCore>
#include <QTimer>
#include <QQmlContext>
#include <QThread>
//#include <modbus.h>
import XYmodbus;
import XYtcpSocket;
import XYopcUa;
import GlobalData;
import ModbusClientThread;
import OpcUaClientThread;

int main(int argc, char *argv[])
{
    //添加一个modbus客户端并在线程内运行（需加载参数建立连接）
    ModbusClientThread modbusClientThread;
    // // 连接modbus读取完成信号,写入全局变量
    QObject::connect(&modbusClientThread, &ModbusClientThread::readCompletedChanged,&GlobalData::instance(), &GlobalData::setMap1ValueSlot, Qt::QueuedConnection);
    // // 连接全局变量写入信号，更新modbus设备数值
    QObject::connect(&GlobalData::instance(), &GlobalData::map1Changed, &modbusClientThread,&ModbusClientThread::writeModbusValuesSignal, Qt::QueuedConnection);//Qt::QueuedConnection
    modbusClientThread.start();

    //socket测试
    // Start the server
    TcpSocketServer socketServer;
    //server.startServer(12345);
    //Start the client
    TcpSocketClient socketClient;
    //Connect the client to the server
   //client.connectToServer("127.0.0.1", 12345);



    //client.sendData("Hello from client!");

    OpcUaClientThread opcUaClientThread;
    //emit opcUaClientThread.connectToEndpoint("abc", 222);








    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreationFailed,
        &app,
        []() { QCoreApplication::exit(-1); },
        Qt::QueuedConnection);
    // 注册 GlobalData 单例到 QML
    engine.rootContext()->setContextProperty("modbusClientThread", &modbusClientThread);
    engine.rootContext()->setContextProperty("socketServer", &socketServer);
    engine.rootContext()->setContextProperty("socketClient", &socketClient);


    qmlRegisterSingletonInstance<GlobalData>("GlobalData", 1, 0, "GlobalData", &GlobalData::instance());
    engine.loadFromModule("apptest", "Main");

    return app.exec();
}
